<?php
/**
 * Created by PhpStorm.
 * User: HeiYanHeiMao
 * Date: 2020/4/28
 * Time: 11:08
 */

namespace app\model;


use think\facade\Db;
use think\response\Json;
use \Exception as E;

class FloorConfModel
{
    private static $_table = 'eb_floor_conf'; // 学校楼层配送费配置
    private static $_schoolTable = 'eb_school'; // 学校
    /**列表
     * @param $input
     * @param $token
     * @return Json
     */
    public static function list($input, $token)
    {
        try {
            // 检测当前管理员状态
            $adminInfo = AdminModel::getInfo($token, [1], [2]);
            if ($adminInfo instanceof Json) return $adminInfo;
            $map = [
                ['operate_id', '=', $adminInfo['id']],
                ['school_id', '=', $input['school_id']]
            ];
            if ($input['name']) $map[] = ['name', 'like', "%{$input['name']}%"];
            $field = 'id,name,delivery, FROM_UNIXTIME(create_time, "%Y-%m-%d %H:%i:%s") AS create_time';
            $table = Db::table(self::$_table)
                ->field($field)->where($map);
            $count = $table->count();
            if ($count == 0) return jsonSuccess('获取成功', ['count' => 0, 'list' => []]);
            $list = $table->page($input['page'], $input['limit'])->order('create_time', 'desc')->select()->toArray();
            return jsonSuccess('获取成功！', ['count' => $count, 'list' => $list]);
        } catch (E $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**添加
     * @param $input
     * @param $token
     * @return Json
     */
    public static function add($input, $token)
    {
        try {
            // 检测当前管理员状态
            $adminInfo = AdminModel::getInfo($token, [1], [2]);
            if ($adminInfo instanceof Json) return $adminInfo;
            $map = [
                ['id', '=', $input['school_id']],
                ['operate_id', '=', $adminInfo['id']]
            ];
            $whereSql = Db::table(self::$_schoolTable)->field('id')->where($map)->fetchSql(true)->find();
            $where = "exists ({$whereSql})";
            $input['operate_id'] = $adminInfo['id'];
            $input['create_time'] = time();
            $sql = makeDualSql(self::$_table, $input, $where);
            $rtn = Db::table(self::$_table)->execute($sql);
            if ($rtn) return jsonSuccess('添加成功');
            return jsonFail('电话已被使用，请使用其他手机号');
        } catch (E $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**删除
     * @param $input
     * @param $token
     * @return Json
     */
    public static function delete($input, $token)
    {
        try {
            // 检测当前管理员状态
            $adminInfo = AdminModel::getInfo($token, [1], [2]);
            if ($adminInfo instanceof Json) return $adminInfo;
            $map = [
                ['id', '=', $input['id']],
                ['school_id', '=', $input['school_id']],
                ['operate_id', '=', $adminInfo['id']]
            ];
            $rtn = Db::table(self::$_table)->where($map)->delete();
            if ($rtn) return jsonSuccess('删除成功');
            return jsonFail('删除失败，请刷新页面重试');
        } catch (E $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**获取单个
     * @param $input
     * @param $token
     * @return Json
     */
    public static function getOnly($input, $token)
    {
        try {
            // 检测当前管理员状态
            $admin = AdminModel::getInfo($token, [1], [2]);
            if ($admin instanceof Json) return $admin;
            $map = [
                ['id', '=', $input['id']],
                ['operate_id', '=', $admin['id']],
                ['school_id', '=', $input['school_id']]
            ];
            $rtn = Db::table(self::$_table)->field('id, name,delivery,school_id')->where($map)->find();
            if ($rtn) return jsonSuccess('获取成功', $rtn);
            return jsonFail('获取失败');
        } catch (E $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**编辑
     * @param $input
     * @param $token
     * @return Json
     */
    public static function edit($input, $token)
    {
        try {
            // 检测当前管理员状态
            $adminInfo = AdminModel::getInfo($token, [1], [2]);
            if ($adminInfo instanceof Json) return $adminInfo;
            $map = [
                ['id', '=', $input['id']],
                ['school_id', '=', $input['school_id']],
                ['operate_id', '=', $adminInfo['id']]
            ];
            unset($input['id']);
            unset($input['school_id']);
            $rtn = Db::table(self::$_table)->where($map)->update($input);
            if ($rtn) return jsonSuccess('修改成功');
            return jsonFail('修改失败，请刷新页面重试');
        } catch (E $e) {
            return jsonFail('服务内部错误~');
        }
    }

    /**获取简单列表
     * @param $input
     * @param $token
     * @return Json
     */
    public static function getSimple($input, $token)
    {
        // 检测当前管理员状态
        $adminInfo = AdminModel::getInfo($token, [1], [2]);
        if ($adminInfo instanceof Json) return $adminInfo;
        $rtn = self::getSimpleStatic($adminInfo['id'], $input['school_id']);
        if (is_array($rtn)) return jsonSuccess('获取成功', $rtn);
        return $rtn;
    }


    /**获取简单列表(内部调用)
     * @param $operateId
     * @param $schoolId
     * @return Json|array
     */
    public static function getSimpleStatic($operateId, $schoolId)
    {
        try {
            $map = [
                ['operate_id', '=', $operateId],
                ['school_id', '=', $schoolId]
            ];
            $rtn = Db::table(self::$_table)->field('id,name,delivery')->where($map)->select()->toArray();
            if ($rtn) return $rtn;
            return jsonFail('未找到可用的配置');
        } catch (E $e) {
            return jsonFail('服务内部错误~');
        }
    }
}